#include<iostream>
#include<cstring>
using namespace std;
class CSet {
private:
	int n;
	int* data;
public:
	CSet(int _n, int* a) {
		n = _n;
		data = new int[n];
		memcpy(data, a, n * sizeof(int));
	}
	void print() {
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				cout << data[i] << endl;
			}
			else {
				cout << data[i] << ' ';
			}
		}
	}
	friend CSet& operator+(CSet& c1, CSet& c2) {
		int* a = new int[c1.n + c2.n];
		memcpy(a, c1.data, c1.n * sizeof(int));
		int n = c1.n;
		for (int i = 0; i < c2.n; i++) {
			int is = 1;
			for (int j = 0; j < c1.n; j++) {
				if (c2.data[i] == c1.data[j]) {
					is = 0;
					break;
				}
			}
			if (is) {
				a[n] = c2.data[i];
				n++;
			}
		}
		CSet* c = new CSet(n, a);
		return *c;
	}
	friend CSet& operator*(CSet& c1, CSet& c2) {
		int n = 0;
		int* a = new int[c1.n];
		if (c1.n > c2.n) {
			for (int i = 0; i < c2.n; i++) {
				for (int j = 0; j < c1.n; j++) {
					if (c2.data[i] == c1.data[j]) {
						a[n] = c2.data[i];
						n++;
						break;
					}
				}
			}
		}
		else {
			for (int i = 0; i < c1.n; i++) {
				for (int j = 0; j < c2.n; j++) {
					if (c1.data[i] == c2.data[j]) {
						a[n] = c1.data[i];
						n++;
						break;
					}
				}
			}
		}
		CSet* c = new CSet(n, a);
		return *c;
	}
	friend CSet& operator-(CSet& c1, CSet& c2) {
		int n = 0;
		int* a = new int[c1.n];
		for (int i = 0; i < c1.n; i++) {
			int is = 1;
			for (int j = 0; j < c2.n; j++) {
				if (c1.data[i] == c2.data[j]) {
					is = 0;
					break;
				}
			}
			if (is) {
				a[n] = c1.data[i];
				n++;
			}
		}
		CSet* c = new CSet(n, a);
		return *c;
	}
};
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		int* a1 = new int[n];
		for (int j = 0; j < n; j++) {
			cin >> a1[j];
		}
		CSet c1(n, a1);
		cin >> n;
		int* a2 = new int[n];
		for (int j = 0; j < n; j++) {
			cin >> a2[j];
		}
		CSet c2(n, a2);
		cout << "A:";
		c1.print();
		cout << "B:";
		c2.print();
		cout << "A+B:";
		(c1 + c2).print();
		cout << "A*B:";
		(c1 * c2).print();
		cout << "(A-B)+(B-A):";
		((c1 - c2) + (c2 - c1)).print();
		cout << endl;
	}
	return 0;
}